$(function () {
    function getList() {

        $.ajax({
            type: 'GET',
            url: 'http://localhost:8080/api/v1/admin/category/list',
            headers: {
                Authorization: localStorage.getItem('token')
            },                   
            success: function (resData) {
                console.log(resData);
                if (resData.code === 200) {
                    console.log('成功');
                    // layer.close(index);
                    let trHtml = template('art_list', resData);
                    $('tbody').html(trHtml);
                } else {
                    console.log('不成功');
                }
                
            }
        })
    }

    getList()

    //新增模态框
    $('#xinzengfenlei').on('click', function () {
        $('#myModal').modal('show')
    })

    //新增分类
    $('#btnSave').on('click', function () {
        let name = $("[name = 'name']").val().trim()
        let slug = $("[name = 'slug']").val().trim()

        $.ajax({
            url: 'http://localhost:8080/api/v1/admin/category/add',
            data: { name, slug },
            //需要请求头带上token
            headers: {
                Authorization: localStorage.getItem('token')
            },   
            type: 'post',
            success: function () {
                $("[name = 'name']").val('')
                $("[name = 'slug']").val('')
                $('#myModal').modal('hide')
                getList()
            }
        })
    })

    let editId
    // 点击编辑按钮，事件委托
    $('tbody').on('click', '.edit', function () {
        $('#editModal').modal('show')
        let value = $(this).parents('tr').data('value')
        console.log(value);
        editId = value.id
        $('#name').val(value.name)
        $('#slug').val(value.slug)
    })

    //编辑保存
    $('#editModal .btn_opt').on('click', function () {
        let name = $('#name').val()
        let slug = $('#slug').val()

        $.ajax({
            url: 'http://localhost:8080/api/v1/admin/category/edit',
            type: 'post',
            data: {
                id: editId,
                name,
                slug
            },
            headers: {
                Authorization:localStorage.getItem('token')
            },
            success: function (res) {
                if (res.code === 200) {
                    console.log(res.msg);
                    $('#editModal').modal('hide')
                    getList()
                } else {
                    console.log('失败了');
                    console.log(res);
                }
            }

        })
    })

    // 删除
    $('tbody').on('click', '.delete', function () {
        if (confirm('真的要删除吗？')) {
            let value = $(this).parents('tr').data('value')
            let id =value.id
            $.ajax({
                url: 'http://localhost:8080/api/v1/admin/category/delete',
                type: 'post',
                headers: {
                    Authorization:localStorage.getItem('token')
                },
                data: {id},
                success: function (res) {
                    console.log('删除成功');
                    getList()
                 }

                
            })
        }
        
    })
})